<?php
class Article extends AppModel{
    public $useTable  = 'article';
    public $lang_code = 'vn';
    
    
    public function get_article_by_category_id($in_array_cate = array(),  $start = null, $limit = null)
    {
        $lm = "";
        if(($start != null || $start == 0)  && $limit != null)
        {
            $lm = " LIMIT $start, $limit ";
        }
    
        
        if(is_array($in_array_cate))
        {
            $sql_in_array_cate = '0';       
            foreach($in_array_cate as $cate_id)
            {
                if($sql_in_array_cate == '')
                    $sql_in_array_cate .= $cate_id;
                else
                    $sql_in_array_cate .= ', '.$cate_id;                                                        
            }  
            
            $sql_list_cate = ' IN ('.$sql_in_array_cate.')';
        }
        else
        {
            $sql_list_cate = ' = '. $in_array_cate;
        } 
        
        
    	$lang_code = $this->lang_code;
        $sql = "SELECT article.*, article_desc.*, image.`name`, link.*
				FROM article_desc, article
                LEFT JOIN image ON article.article_id = image.post_id AND image.type = 'article' AND image.`status` = 1
                LEFT JOIN link ON article.article_id = link.post_id AND link.lang_code = '$lang_code' AND link.type = 'article'
				WHERE article.article_id = article_desc.article_id AND article.category_id $sql_list_cate 
                AND article.`status` = 1 AND article_desc.lang_code = '$lang_code' 
				ORDER BY article.sort_order, article_desc.title 
                $lm";

		return $this->query($sql);
    }

    public function get_article_by_article_id($article_id = null)
    {
        $lang_code = $this->lang_code;
        $sql = "SELECT article.*, article_desc.*, image.`name`, link.*
				FROM article_desc, article
                LEFT JOIN image ON article.article_id = image.post_id AND image.type = 'article' AND image.`status` = 1
                LEFT JOIN link ON article.article_id = link.post_id AND link.lang_code = '$lang_code' AND link.type = 'article'
				WHERE article.article_id = article_desc.article_id AND article.article_id = '$article_id' 
                AND article.`status` = 1 AND article_desc.lang_code = '$lang_code' 
				ORDER BY article.sort_order, article_desc.title";
        return $this->query($sql);
    }
    
    public function get_number_page_article_by_category_id($in_array_cate = array())
    {
        if(is_array($in_array_cate))
        {
            $sql_in_array_cate = '0';       
            foreach($in_array_cate as $cate_id)
            {
                if($sql_in_array_cate == '')
                    $sql_in_array_cate .= $cate_id;
                else
                    $sql_in_array_cate .= ', '.$cate_id;                                                        
            }  
            
            $sql_list_cate = ' IN ('.$sql_in_array_cate.')';
        }
        else
        {
            $sql_list_cate = ' = '. $in_array_cate;
        }                                                                                       
        $sql = "SELECT COUNT(*) AS count_article FROM article WHERE `status` = 1 AND category_id $sql_list_cate";
        $count = $this->query($sql);
        if(!empty($count))
        {
            $count_article = $count[0][0]['count_article'];
            $limit = Configure::read('post_per_page');
            $number_page = ceil($count_article/$limit);
            $number_page = max(1,$number_page);
            return $number_page;
        }
        
        else
            return 1;
    }
}